import { getPayedBusinessList, getUnPayBusinessList, getUnPaySku, getBusinessOrderList, postCreateBusinessPay, getPayedDetail, getBusinessRefundOrderList  } from '../services/api';

export default {
  namespace: 'businessPay',

  state: {
    unPayBusinessList:[],
    payedBusinessList : {
      list: [],
      pagination: {},
    },
    unPaySkuList: [],
    businessOrderList: [],
    getPayedDetail: {},
    loading : false,
  },

  effects: {
    *fetch({payload}, { call, put }) {
      const response = yield call(getUnPayBusinessList, payload);
      if(response){
        yield put({
          type: 'save',
          payload: {
            unPayBusinessList : response
          },
        });
      }
    },
    *fetchPayed({payload}, { call, put }) {
      const response = yield call(getPayedBusinessList, payload);
      if(response){
        yield put({
          type: 'save',
          payload: {
            payedBusinessList : response
          },
        });
      }
    },
    *fetchUnPaySku({payload}, { call, put }) {
      const response = yield call(getUnPaySku, payload);
      if(response){
        yield put({
          type: 'save',
          payload: {
            unPaySkuList : response
          },
        });
      }
    },
    *fetchBusinessOrderList({payload}, { call, put }) {
      const response = yield call(getBusinessOrderList, payload);
      if(response){
        yield put({
          type: 'save',
          payload: {
            businessOrderList : response
          },
        });
        return response || [];
      }
    },
    *fetchBusinessRefundOrder({payload}, { call, put }) {
      const response = yield call(getBusinessRefundOrderList, payload);
      if(response){
        // 前端处理异常订单添加状态为0，用于区分异常订单
        const data = response.map(v=>({
          ...v,
          status : 0,
        }));
        yield put({
          type: 'saveRefundOrder',
          payload: {
            data : data
          },
        });
        return data || [];
      }
    },
    *createBusinessPay({payload}, { call, put }) {
      const response = yield call(postCreateBusinessPay, payload);
      if(response){
        return response || [];
      }
    },
  },

  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
    saveRefundOrder(state, {payload}) {
      // 组合异常订单与正常订单'
      const temp = [
        ...payload.data,
        ...state.businessOrderList,
      ];
      return {
        ...state,
        businessOrderList : temp,
      }
    },
    clear() {
      return {

        loading: false,

      };
    },
  },
};
